<?php


namespace App\Services\Formatter;


use App\Model\ChatInfo;
use App\Model\User;
use Carbon\Carbon;

class ChatFormatter
{
    public function stdoutFormatter(ChatInfo $model, int $meetingCreator = 0): array
    {
        if ($model->sender_id > 0) {
            if ($model->sender_id == $meetingCreator) {
                $sender = "主持人";
            } else {
                $sender = User::where('id', '=', $model->sender_id)->value('nickname');
            }
        } elseif ($model->sender_id == 0) {
            $sender = '系统';
        } else {
            $sender = $model->sender_name;
        }
        if ($model->recv_id > 0) {
            $recv = User::where('id', '=', $model->recv_id)->value('nickname');
        } else {
            $recv = '';
        }
        return [
            'id' => $model->id,
            'senderId' => $model->sender_id,
            'senderName' => $sender,
            'recvId' => $model->recv_id,
            'recvName' => $recv,
            'content' => $model->content,
            'msgType' =>$model->msg_type,
            'meetingId' => $model->meeting_id,
            'sendTime' => Carbon::parse($model->send_time)->format('Y-m-d H:i:s'),
            'createTime' => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'),
            'updateTime' => Carbon::parse($model->updated_at)->format('Y-m-d H:i:s'),
        ];
    }

    public function formatList($models, int $meetingCreator = 0): array
    {
        $result = [];
        foreach ($models as $model) {
            /* @var $model ChatInfo */
            $result[] = $this->stdoutFormatter($model, $meetingCreator);
        }
        return $result;
    }
}
